/**
 * Copyright &copy; 2016 splat  All rights reserved.
 */
package com.contractpage.util;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.common.util.ConfigUtil;
import com.rbac.entity.RbacResources;
import com.rbac.service.PrivilegeService;
import com.richgo.util.Tool;
import com.user.entity.emp.EmpVO;


/**
 * 用户工具类
 * @author splat
 * @version 2013-12-05
 */
public class UserUtils {
	
    private static PrivilegeService privilegeService = SpringContextHolder.getBean(PrivilegeService.class);;
	
	/**
	 * 获取当前用户
	 * @return 取不到返回 new EmpVO()
	 */
	public static EmpVO getEmpVO(){
		Object obj = WebContextHolder.getSession().getAttribute("user");
		if(obj != null){
			return (EmpVO)obj;
		}
		return null;
	}

	public static EmpVO getUser(){
		Object obj = WebContextHolder.getSession().getAttribute("user");
		if(obj != null){
			return (EmpVO)obj;
		}
		return new EmpVO();
	}
	
	
	/**
	 * 获取当前用户授权菜单，菜单栏显示
	 * @return
	 */
	public static List<RbacResources> getMenuList(){
		List<RbacResources> resourceslist= privilegeService.getResourcesByUserId(Integer.parseInt(ConfigUtil.getValue("rbac.system")), UserUtils.getUser().getCode());
        //SxyUser sxyUser = sxyUserService.getUserByCode(getEmpVO().getCode());
        /*if(sxyUser == null){
        	sxyUser.setr
        }*/
		return resourceslist;
	}
	/*public static  ResourcessVO getMenuInfoList(){
		ResourcessVO res = new ResourcessVO();
		List<RbacResources> resourceslist= privilegeService.getResourcesByUserId(Integer.parseInt(ConfigUtil.getValue("rbac.system")), UserUtils.getUser().getCode());
        SxyUser sxyUser = sxyUserService.getUserByCode(getEmpVO().getCode());
        if(sxyUser == null){
        	res.setResult("10000");
        	return res;
        }
        if("1".equals(sxyUser.getUserstatus())){
        	res.setResult("20000");
        	return res;
        }
        int count = sxyUserService.getUserByParentCode();
        System.out.println(count);
        if(count ==0){
        	 for (int i = 0; i < resourceslist.size(); i++) {
             	String url = resourceslist.get(i).getResUrl();
             	if(url.contains("activity/subActivity.html")){
             		resourceslist.remove(i);
             		break;
             	}
     		}
        }
        res.setResult("00000");
        res.setResourceslist(resourceslist);
    	return res;
		
	}*/
	/**
	 * 获取按钮
	 * @return
	 */
	/*public static List<RbacResources> getRbacButton(){
		ResourcessVO  res = null;
		res = (ResourcessVO) RedisClusterUtil.get("sxybotton", ResourcessVO.class);
		if(res == null){
			res=getMenuInfoList();
			RedisClusterUtil.set("sxybotton", res);
		}
		List<RbacResources> list = new ArrayList<RbacResources>();
		String [] bobbon = null;
		if(res!=null){
			if(res.getResourceslist().size()>0){
				List<RbacResources> bolist= res.getResourceslist();
				for (int i = 0; i < bolist.size(); i++) {
					System.out.println(bolist.get(i).getResName()+":"+bolist.get(i).getResType());
					if(bolist.get(i).getResType() ==2){
						bobbon[i] = bolist.get(i).getResKey();
					}
				}
			}
		}
		return list;
	}*/
	/**
	 * 获取系统主页
	 * @return
	 */
	public static String getmainUrl(){
	    String url = ConfigUtil.getValue("sso.web.site");
	    return url;
	}
	
	/**
	 * 获取当前用户授权菜单admin根据平台获取菜单,菜单管理
	 * @return
	 */
	/*public static List<Menu> getMenuList(String platType){
		@SuppressWarnings("unchecked")
		List<Menu> menuList = null;//(List<Menu>)getCache(platType);
		if (menuList == null){
			EmpVO EmpVO = getEmpVO();
			Menu m = new Menu();
			//A平台过来去掉权限验证
			if (EmpVO.isAdmin()){
				m.setPlatType(platType);
				menuList = menuDao.findAllList(m);
			}else{
				m.setEmpVOId(EmpVO.getId());
				menuList = menuDao.findByEmpVOId(m);
			}
			//menuList = menuDao.findAllList(new Menu());
			putCache(platType, menuList);
		}
		return menuList;
	}
	*/
	
	/**
	 * 获取当前用户授权的区域
	 * @return
	 */
	/*public static List<Area> getAreaList(){
		@SuppressWarnings("unchecked")
		List<Area> areaList = null;//(List<Area>)getCache(CACHE_AREA_LIST);
		if (areaList == null){
			areaList = areaDao.findAllList(new Area());
			putCache(CACHE_AREA_LIST, areaList);
		}
		return areaList;
	}*/
	
	/**
	 * 获取当前用户有权限访问的部门
	 * @return
	 */
	/*public static List<Office> getOfficeList(){
		@SuppressWarnings("unchecked")
		List<Office> officeList = null;//(List<Office>)getCache(CACHE_OFFICE_LIST);
		if (officeList == null){
			EmpVO EmpVO = getEmpVO();
			if (EmpVO.isAdmin()){//为管理员时查询所有
				officeList = officeDao.findAllList(new Office());
			}else{
				Office office = new Office();
				office.setParent(EmpVO.getCompany());//设置当前用户能拿到的机构权限
				//office.getSqlMap().put("dsf", BaseService.dataScopeFilter(EmpVO, "a", ""));//设置数据范围sql
				officeList = officeDao.findList(office);
			}
			putCache(CACHE_OFFICE_LIST, officeList);
		}
		return officeList;
	}*/

	/**
	 * 获取所有机构
	 * @return
	 */
	/*public static List<Office> getOfficeAllList(){
		@SuppressWarnings("unchecked")
		List<Office> officeList = null;//(List<Office>)getCache(CACHE_OFFICE_ALL_LIST);
		if (officeList == null){
			officeList = officeDao.findAllList(new Office());
			putCache("CACHE_OFFICE_ALL_LIST",officeList);
		}
		return officeList;
	}*/
	
	
	/*public static EmpVO getUser(){
		return getPrincipal().getEmpV0();
	}*/
	
	
	
	// ============== EmpVO Cache ==============
	
	

	
//	public static Map<String, Object> getCacheMap(){
//		Principal principal = getPrincipal();
//		if(principal!=null){
//			return principal.getCacheMap();
//		}
//		return new HashMap<String, Object>();
//	}
	
}
